## Paper: "Politically Heterogeneous Acquaintances and Close Relationships 
###        are Widespread, and Relate to Lower Partisan Animosity"
## Replication data: Appendix
## May, 2025

### Appendix A: Sample characteristics compared to US census 
## (Taken from Druckman et al 2022, appendix, page 1) 


prop.table(table(data$gender))

data$age_benchmark <- NA
data$age_benchmark[data$age < 25] <- "18-24"
data$age_benchmark[data$age > 24 & data$age < 35] <- "25-34"
data$age_benchmark[data$age > 34 & data$age < 51] <- "35-50"
data$age_benchmark[data$age > 50 & data$age < 66] <- "51-65"
data$age_benchmark[data$age > 65] <- "Over 65"

prop.table(table(data$age_benchmark))

prop.table(table(data$race))

prop.table(table(data$hispanic))

prop.table(table(data$education))

# Income study 1
data$income_benchmark <- NA
data$income_benchmark[data$family_income == "Less than $10,000" |
                        data$family_income == "$10,000 to $19,999" |
                        data$family_income == "$20,000 to $29,999"] <- "30.000 or less"
data$income_benchmark[data$family_income == "$30,000 to $39,999" |
                        data$family_income == "$40,000 to $49,999" |
                        data$family_income == "$50,000 to $59,999" |
                        data$family_income == "$60,000 to $69,999"] <- "30.000 - 69.999"
data$income_benchmark[data$family_income == "$70,000 to $79,999" |
                        data$family_income == "$80,000 to $89,999" |
                        data$family_income == "$90,000 to $99,999"] <- "70.000 - 99.999"
data$income_benchmark[data$family_income == "$100,000 to $149,999" |
                        data$family_income == "$150,000 or more"] <- "100.000 or more"

prop.table(table(data$income_benchmark))


### Sample characteristics of Study 2 (misperceptions part of the paper)

prop.table(table(data2$gender))

data2$age_benchmark <- NA
data2$age_benchmark[data2$age < 25] <- "18-24"
data2$age_benchmark[data2$age > 24 & data2$age < 35] <- "25-34"
data2$age_benchmark[data2$age > 34 & data2$age < 51] <- "35-50"
data2$age_benchmark[data2$age > 50 & data2$age < 66] <- "51-65"
data2$age_benchmark[data2$age > 65] <- "Over 65"

prop.table(table(data2$age_benchmark))

prop.table(table(data2$race))

prop.table(table(data2$hispanic))

prop.table(table(data2$education))

data2$income_benchmark <- NA
data2$income_benchmark[data2$family_income == "Less than $10,000" |
                         data2$family_income == "$10,000 to $19,999" |
                         data2$family_income == "$20,000 to $29,999"] <- "30.000 or less"
data2$income_benchmark[data2$family_income == "$30,000 to $39,999" |
                         data2$family_income == "$40,000 to $49,999" |
                         data2$family_income == "$50,000 to $59,999" |
                         data2$family_income == "$60,000 to $69,999"] <- "30.000 - 69.999"
data2$income_benchmark[data2$family_income == "$70,000 to $84,999" |
                         data2$family_income == "$85,000 to $99,999"] <- "70.000 - 99.999"
data2$income_benchmark[data2$family_income == "$100,000 to $149,999" |
                         data2$family_income == "$150,000 or more"] <- "100.000 or more"

prop.table(table(data2$income_benchmark))






###################################################################
################# NETWORK HETEROGENEITY AND ANIMOSITY #############
###################################################################




## Regression models network heterogeneity and animosity

Affect_close <- lm(outparty_final ~ close_network + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, 
                   data = data)

Affect_ARD_LB <- lm(outparty_final ~ Acquaintance_dummy + Gender + Age + Education + Race
                    + Ideology + Political_interest + Strong_partisan, 
                    data = data)

Affect_close_acqcontrol <- lm(outparty_final ~ close_network + Acquaintance_dummy + Gender + Age + Education + Race
                              + Ideology + Political_interest + Strong_partisan, 
                              data = data)

Fig_4 <- texreg(l = list(Affect_close, Affect_ARD_LB, Affect_close_acqcontrol),
                use.packages = F,
                stars = c(0.05, 0.01, 0.001),
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Close ntw heterogeneity", "Acq ntw heterogeneity", "Close and acq ntw"),
                threeparttable = TRUE,
                caption.above=T,
                caption="Table A1: Linear regression models capturing the relationship between out-partisan animosity and
network heterogeneity. Figure 3 in the main document.",
                float.pos="hp!")

## Express results in SD

data_std <- data  # Copy of dataset

# Standardize only the dependent variable
data_std$outparty_final <- scale(data_std$outparty_final, center = TRUE, scale = TRUE)

# Run regression without standardizing the dummy variable
Affect_close_std <- lm(outparty_final ~ close_network + Gender + Age + Education + Race
                       + Ideology + Political_interest + Strong_partisan, 
                       data = data_std)

summary(Affect_close_std)  # View results

Affect_ARD_LB_std <- lm(outparty_final ~ Acquaintance_dummy + Gender + Age + Education + Race
                       + Ideology + Political_interest + Strong_partisan, 
                       data = data_std)

summary(Affect_ARD_LB_std)  # View results


## Robustness: IP - AP outcome measure

AP_close <- lm(AP_final ~ close_network + Gender + Age + Education + Race
               + Ideology + Political_interest + Strong_partisan, 
               data = data)

AP_Acquaintance_heterogeneity <- lm(AP_final ~ Acquaintance_dummy + Gender + Age + Education + Race
                                    + Ideology + Political_interest + Strong_partisan, 
                                    data = data)

AP_close_acqcontrol <- lm(AP_final ~ close_network + Acquaintance_dummy + Gender + Age + Education + Race
                          + Ideology + Political_interest + Strong_partisan, 
                          data = data)

Fig_5 <- texreg(l = list(AP_close, AP_Acquaintance_heterogeneity, AP_close_acqcontrol),
                use.packages = F,
                stars = c(0.05, 0.01, 0.001),
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Close ntw heterogeneity", "Acq ntw heterogeneity", "Close and acq ntw"),
                threeparttable = TRUE,
                caption.above=T,
                caption="Robustness Check. Linear regression models capturing the relationship between outpartisan
animosity and network heterogeneity using a measure of affective polarization that subtracts
the index of out=partisan thermometer scores, traits and trust used in the main paper,
from an index of in-partisan thermometer scores, traits and trust items. Where in the out-partisan
measure, positive coefficients denote lower out-partisan animosity, in the table below, positive coefficients
denote higher affective polarization. The table thus shows that respondents embedded in
both heterogeneous close and acquaintance networks report lower levels of affective polarization.",
                float.pos="hp!")

## Robustness: non-dichotomous measures of independent variables: 
# Proportion of outpartisan acquaintances, and number of close outpartisan ties

Affect_ARD_LB_prop <- lm(outparty_final ~ ard_LB + Gender + Age + Education + Race
                        + Ideology + Political_interest + Strong_partisan, 
                        data = data)

Affect_close_number <- lm(outparty_final ~ close_network_numeric + Gender + Age + Education + Race
                                  + Ideology + Political_interest + Strong_partisan, 
                                  data = data)


Fig_6 <- texreg(l = list(Affect_ARD_LB_prop, Affect_close_number),
                use.packages = F,
                stars = c(0.05, 0.01, 0.001),
                digits=3, include.ci = FALSE, booktabs=T,
                custom.model.names = c("Proportion Acq.", "Number of out-partisan Close Ties"),
                threeparttable = TRUE,
                caption.above=T,
                caption="Robustness Check. Linear regression models capturing the relationship between outpartisan
animosity and network heterogeneity using a measure of acquaintance network heterogeneity that takes the 
proportion of out-partisan acquaintances, and using a measure of close network heterogeneity that counts the 
number of close out-partisan ties. The table shows that with these measures too, respondents embedded in
both heterogeneous close and acquaintance networks report lower levels of affective polarization.",
                float.pos="hp!")



              ########################################################### 
              #####  PUZZLE BY NETWORK HETEROGENEITY ####################
              ###########################################################




Puzzle_close_appendix <- lm(outcome_puzzle ~ treatment_puzzle * close_network + 
                              Gender + Age + Education + Race + Ideology + Political_interest 
                            + Strong_partisan, data = data)


Puzzle_acq_appendix <- lm(outcome_puzzle ~ treatment_puzzle * Acquaintance_dummy + 
                            Gender + Age + Education + Race + Ideology + Political_interest 
                          + Strong_partisan, data = data)


puzzle_networks_appendix <- texreg(l = list(Puzzle_close_appendix, Puzzle_acq_appendix),
                                   use.packages = F,
                                   stars = c(0.05, 0.01, 0.001),
                                   digits=3, include.ci = FALSE, booktabs=T,
                                   custom.model.names = c("Close-tie network", "Acquaintance network"),
                                   threeparttable = TRUE,
                                   caption.above=T,
                                   caption="Regression model estimating the likelihood of choosing an engineer in the competence
game by close-tie and acquaintance network heterogeneity. Figure 4 in the main document.",
                                   float.pos="hp!")


# Robustness with continuous acq measure

Puzzle_acq_appendix_ard <- lm(outcome_puzzle ~ treatment_puzzle * ard_LB + 
                                Gender + Age + Education + Race + Ideology + Political_interest 
                              + Strong_partisan, data = data)






########################################################
############# HETEROGENEITY AND MISPERCEPTIONS #########
########################################################



## Regression table figure 5 in the paper

MMAll_propLB <- lm(mean_error_all ~ Acquaintance_heterogeneity + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

MMAll_propLB_ard <- lm(mean_error_all ~ Acquaintance_dummy + Gender + Age + Education + Race
                       + Ideology + Political_interest + Strong_partisan, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

Figure_9_paper <- texreg(l = list(MMAll_propLB, MMAll_propLB_ard),
                         use.packages = F,
                         stars = c(0.05, 0.01, 0.001),
                         digits=3, include.ci = FALSE, booktabs=T,
                         custom.model.names = c("Continuous acq. measure", "Binary acq. measure"),
                         threeparttable = TRUE,
                         caption.above=T,
                         caption="Linear regression model estimating the relationship between heterogeneous acquaintance
networks and misperceptions. Figure 5A in the main paper.",
                         float.pos="hp!")

## Robustness with different measures (Only outparty, Ahler and Sood, Ahler and Sood + only outparty)

MMout_propLB <- lm(mean_error_outparty ~ Acquaintance_heterogeneity + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

MMall_propLB_AS <- lm(mean_error_all_AS ~ Acquaintance_heterogeneity + Gender + Age + Education + Race
                      + Ideology + Political_interest + Strong_partisan, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

MMout_propLB_AS <- lm(mean_error_outparty_AS ~ Acquaintance_heterogeneity + Gender + Age + Education + Race
                      + Ideology + Political_interest + Strong_partisan, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

robustness_fig9 <- texreg(l = list(MMout_propLB, MMall_propLB_AS, MMout_propLB_AS),
                          use.packages = F,
                          stars = c(0.05, 0.01, 0.001),
                          digits=3, include.ci = FALSE, booktabs=T,
                          custom.model.names = c("Outparty only", "Ahler and Sood", "A.S. outparty only"),
                          threeparttable = TRUE,
                          caption.above=T,
                          caption= "Robustness check: Linear regression model estimating the relationship between heterogeneous
acquaintance networks and misperceptions, considering 3 alternative ways of measuring
misperceptions. Namely, the misperception measure used in the paper, but only considering misperceptions
of the outparty. The second and third models use the measure by Ahler and Sood,
which is only about the social composition of in- and out-partisans. All models show that a higher
proportion of out-partisan acquaintances is significantly associated with lower misperceptions, and
effects are larger than in our main model in the paper.",
                          float.pos="hp!")



# Misperceptions and animosity

MMAll_affect <- lm(outparty_final ~ mean_error_all + Gender + Age + Education + Race
                   + Ideology + Strong_partisan + Political_interest, data = subset(data2, ardLB_outpartyties > 0 & ardLB_inpartyties > 0))

predicted_effect <- ggpredict(MMAll_affect, terms = "mean_error_all")

Misp_affect_plot <- ggplot(predicted_effect, aes(x = x, y = predicted)) +
  geom_line(color = "black", size = 1) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high), alpha = 0.2) +
  labs(
    y = "Out-partisan affect", 
    x = "Predicted mean error (* 100%)"
  ) +
  ylim(0.2, 0.5) +
  theme(
    plot.title = element_text(size = 20, face = "bold", hjust = 0.5),
    text = element_text(size = 20, colour = "black"),
    axis.text = element_text(size = 20, color = "black"),
    axis.title.y = element_text(size = 20, color = "black", face = "bold"),
    axis.title.x = element_text(size = 20, color = "black", face = "italic")
  )


#################################
########### EXPERIMENT ##########
#################################



## Randomization check: Treatment assignment by covariates

data2$Treatment <- as.factor(data2$Treatment)

data2$Treatment_num <- ifelse(data2$Treatment == "Treatment", 1, 0)

randomization <- lm(Treatment_num ~ Gender + Age + Education + Race
                    + Ideology + Political_interest + Strong_partisan, data = data2)

custom.names <- c("(Intercept)" = "Intercept",
                  "GenderMale" = "Male",
                  "Age" = "Age",
                  "EducationSome College" = "Some College",
                  "RaceHispanic any race" = "Hispanic",
                  "Racenon-Hispanic Asian" = "Asian",
                  "Racenon-Hispanic Black" = "Black",
                  "Racenon-Hispanic Some Other race" = "Some other race",
                  "IdeologySlightly liberal" = "Slightly liberal",
                  "IdeologyLiberal" = "Liberal",
                  "IdeologyExtremely liberal" = "Extremely liberal",
                  "IdeologySlightly conservative" = "Slightly conservative",
                  "IdeologyConservative" = "Conservative",
                  "IdeologyExtremely conservative" = "Extremely conservative",
                  "Political_interest" = "Political interest",
                  "Strong_partisan" = "Strong partisan")

randomization <- texreg(l = list(randomization),
                        use.packages = F,
                        stars = c(0.05, 0.01, 0.001),
                        digits=3, include.ci = FALSE, booktabs=T,
                        threeparttable = TRUE,
                        caption.above=T,
                        caption= "Randomization check for the experimental manipulation testing for the seeing-the-other-side mechanism",
                        float.pos="hp!",
                        custom.coef.names = custom.names)

## Do respondents in general see their ties as more similar or more different?


## Distributions of mostly similar/mostly different


data2$t_income_dist <- factor(data2$treatment_1, ordered = TRUE, exclude = NA,
                              levels = c("Very different",
                                         "Quite different",
                                         "Quite similar",
                                         "Very similar"))

data2$t_Political_interest_dist <- factor(data2$treatment_2, ordered = TRUE, exclude = NA,
                                          levels = c("Very different",
                                                     "Quite different",
                                                     "Quite similar",
                                                     "Very similar"))

data2$t_poldiscussion_dist <- factor(data2$treatment_3, ordered = TRUE, exclude = NA,
                                     levels = c("Very different",
                                                "Quite different",
                                                "Quite similar",
                                                "Very similar"))

data2$t_countrylove_dist <- factor(data2$treatment_4, ordered = TRUE, exclude = NA,
                                   levels = c("Very different",
                                              "Quite different",
                                              "Quite similar",
                                              "Very similar"))


t_income_dist_plot <- data2 %>% 
  filter(t_income_dist != "NA") %>% 
  ggplot(aes(x = t_income_dist)) +
  geom_bar(fill = "darkred") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("Perceived similarity/difference in income") + 
  ylab("Count")  + 
  theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 

t_Political_interest_dist_plot <- data2 %>% 
  filter(t_Political_interest_dist != "NA") %>% 
  ggplot(aes(x = t_Political_interest_dist)) +
  geom_bar(fill = "darkred") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("Perceived similarity/difference in political interest") + 
  ylab("Count")  + 
  theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 

t_poldiscussion_dist_plot <- data2 %>% 
  filter(t_poldiscussion_dist != "NA") %>% 
  ggplot(aes(x = t_poldiscussion_dist)) +
  geom_bar(fill = "darkred") + 
  geom_bar(fill = "darkred") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("Perceived similarity/difference in frequency of political discussion") + 
  ylab("Count")  + 
  theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 

t_countrylove_dist_plot <- data2 %>% 
  filter(t_countrylove_dist != "NA") %>% 
  ggplot(aes(x = t_countrylove_dist)) +
  geom_bar(fill = "darkred") + 
  geom_bar(fill = "darkred") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("Perceived similarity/difference in love for the country") + 
  ylab("Count")  + 
  theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 


## Do those in heterogeneous acquaintance networks see their 
# in- and outpartisan ties as more similar?

treatment_similarity_heterogeneity <- lm(total_similarity ~ Acquaintance_dummy + Gender + Age 
                                         + Education + Race + Ideology + Political_interest 
                                         + Strong_partisan, data = data2)

custom.names <- c("(Intercept)" = "Intercept",
                  "Acquaintance_dummy" = "Heterogeneous acquaintance network",
                  "GenderMale" = "Male",
                  "Age" = "Age",
                  "EducationSome College" = "Some College",
                  "RaceHispanic any race" = "Hispanic",
                  "Racenon-Hispanic Asian" = "Asian",
                  "Racenon-Hispanic Black" = "Black",
                  "Racenon-Hispanic Some Other race" = "Some other race",
                  "IdeologySlightly liberal" = "Slightly liberal",
                  "IdeologyLiberal" = "Liberal",
                  "IdeologyExtremely liberal" = "Extremely liberal",
                  "IdeologySlightly conservative" = "Slightly conservative",
                  "IdeologyConservative" = "Conservative",
                  "IdeologyExtremely conservative" = "Extremely conservative",
                  "Political_interest" = "Political interest",
                  "Strong_partisan" = "Strong partisan")


treatment_similarity_heterogeneity <- texreg(l = list(treatment_similarity_heterogeneity),
                                             use.packages = F,
                                             stars = c(0.05, 0.01, 0.001),
                                             digits=3, include.ci = FALSE, booktabs=T,
                                             threeparttable = TRUE,
                                             caption.above=T,
                                             caption= "Linear regression modeling levels of perceived similarity between in- and out-partisan
                        acquaintances as a function of acquaintance network heterogeneity. Results show that respondents
                        in heterogeneous networks see their acquaintances as more similar than respondents in homogeneous
                        networks. The measure of perceived similarity is obtained as the sum of the responses over the four
                        characteristics. Response options (very different, quite different, quite similar, very similar) were
                        assigned numeric values -2, -1, 1, 2, respectively. The final variable thus ranges from -8 (acquaintances
                        very different on all characteristics) to 8 (acquaintances very similar on all characteristics).
                        The mean level of this variable is 0.72, with an SD of 3.75.",
                                             float.pos="hp!",
                                             custom.coef.names = custom.names)


### Main treatment effect

ATE_one_tie <- lm(outparty_final ~ Treatment + Gender + Age + Education + Race
                  + Ideology + Political_interest + Strong_partisan, 
                  data = subset(data2, ardLB_inpartyties > 0 & ardLB_outpartyties > 0))

custom.names <- c("(Intercept)" = "Intercept",
                  "TreatmentTreatment" = "Treatment: similarity assessment",
                  "GenderMale" = "Male",
                  "Age" = "Age",
                  "EducationSome College" = "Some College",
                  "RaceHispanic any race" = "Hispanic",
                  "Racenon-Hispanic Asian" = "Asian",
                  "Racenon-Hispanic Black" = "Black",
                  "Racenon-Hispanic Some Other race" = "Some other race",
                  "IdeologySlightly liberal" = "Slightly liberal",
                  "IdeologyLiberal" = "Liberal",
                  "IdeologyExtremely liberal" = "Extremely liberal",
                  "IdeologySlightly conservative" = "Slightly conservative",
                  "IdeologyConservative" = "Conservative",
                  "IdeologyExtremely conservative" = "Extremely conservative",
                  "Political_interest" = "Political interest",
                  "Strong_partisan" = "Strong partisan")

Fig_10 <- texreg(l = list(ATE_one_tie),
                 use.packages = F,
                 stars = c(0.05, 0.01, 0.001),
                 digits=3, include.ci = FALSE, booktabs=T,
                 custom.model.names = c("Treatment effect"),
                 threeparttable = TRUE,
                 caption.above=T,
                 caption= "Linear regression modeling the effect of the treatment (assessing similarity manipulation)
on levels of out-partisan animosity. Figure 9B in the main text.",
                 float.pos="hp!",
                 custom.coef.names = custom.names)


### Other pre-registered hypotheses

## Treatment effect by acquaintance network heterogeneity, for respondents with more than 2 outparty ties

ATE_two_ties_ard <- lm(outparty_final ~ Treatment * Acquaintance_dummy + Gender + Age + Education + Race
                       + Ideology + Political_interest + Strong_partisan, 
                       data = subset(data2, ardLB_outpartyties > 1))

## Treatment effect by whether respondents report differences or similarities

ATE_two_ties_similar <- lm(outparty_final ~ Treatment * total_similarity_dummy_2 + Gender + Age + Education + Race
                           + Ideology + Political_interest + Strong_partisan, 
                           data = subset(data2, ardLB_outpartyties > 1))

## Appendix model

Fig_10_robust <- texreg(l = list(ATE_two_ties_ard, ATE_two_ties_similar),
                        use.packages = F,
                        stars = c(0.05, 0.01, 0.001),
                        digits=3, include.ci = FALSE, booktabs=T,
                        custom.model.names = c("By acq. ntw. heterogeneity", "By perceived similarity"),
                        threeparttable = TRUE,
                        caption.above=T,
                        caption="Robustness check: Linear regressions modeling the effect of the treatment (assessing
similarity manipulation) on levels of out-partisan animosity for respondents who report at least two
out-partisan acquaintance ties. The first model assesses whether the effect of the treatment depends
on a respondent's acquaintance network heterogeneity. The second model tests the effect of the
treatment by whether respondents rate their Democratic and Republican acquaintance networks
as more similar (1) or more different (0). In both cases, we do not find heterogeneity in treatment
effects.",
                        float.pos="hp!")

#print(Fig_10_robust, file = "C:/Users/user/Documents/EUI/US_Paper/Figures in paper/Fig_10_robust.tex")



















#######################################################################################
############## HEARING THE OTHER SIDE: TALKING POLITICS AND ANIMOSITY #################
#######################################################################################












## Mechanisms

## Limit data to people with at least one outpartisan close tie

x <- data %>% filter(data$a1outparty == "1" | data$a2outparty == "1" | data$a3outparty == "1" |
                       data$a4outparty == "1" | data$a5outparty == "1")

## Make mechanism variables for each tie

#a1
x$a1different <- NA
x$a1different[x$all_partisanship == "Democrat"] <- 
  x$a1mechanisms_1.1[x$all_partisanship == "Democrat"]
x$a1different[x$all_partisanship == "Republican"] <- 
  x$a1mechanisms_1[x$all_partisanship == "Republican"]

x$a1disagree <- NA
x$a1disagree[x$all_partisanship == "Democrat"] <- 
  x$a1mechanisms_2.1[x$all_partisanship == "Democrat"]
x$a1disagree[x$all_partisanship == "Republican"] <- 
  x$a1mechanisms_2[x$all_partisanship == "Republican"]

x$a1understand <- NA
x$a1understand[x$all_partisanship == "Democrat"] <- 
  x$a1mechanisms_3.1[x$all_partisanship == "Democrat"]
x$a1understand[x$all_partisanship == "Republican"] <- 
  x$a1mechanisms_3[x$all_partisanship == "Republican"]

# a2
x$a2different <- NA
x$a2different[x$all_partisanship == "Democrat"] <- 
  x$a2mechanisms_1.1[x$all_partisanship == "Democrat"]
x$a2different[x$all_partisanship == "Republican"] <- 
  x$a2mechanisms_1[x$all_partisanship == "Republican"]

x$a2disagree <- NA
x$a2disagree[x$all_partisanship == "Democrat"] <- 
  x$a2mechanisms_2.1[x$all_partisanship == "Democrat"]
x$a2disagree[x$all_partisanship == "Republican"] <- 
  x$a2mechanisms_2[x$all_partisanship == "Republican"]

x$a2understand <- NA
x$a2understand[x$all_partisanship == "Democrat"] <- 
  x$a2mechanisms_3.1[x$all_partisanship == "Democrat"]
x$a2understand[x$all_partisanship == "Republican"] <- 
  x$a2mechanisms_3[x$all_partisanship == "Republican"]

# a3
x$a3different <- NA
x$a3different[x$all_partisanship == "Democrat"] <- 
  x$a3mechanisms_1.1[x$all_partisanship == "Democrat"]
x$a3different[x$all_partisanship == "Republican"] <- 
  x$a3mechanisms_1[x$all_partisanship == "Republican"]

x$a3disagree <- NA
x$a3disagree[x$all_partisanship == "Democrat"] <- 
  x$a3mechanisms_2.1[x$all_partisanship == "Democrat"]
x$a3disagree[x$all_partisanship == "Republican"] <- 
  x$a3mechanisms_2[x$all_partisanship == "Republican"]

x$a3understand <- NA
x$a3understand[x$all_partisanship == "Democrat"] <- 
  x$a3mechanisms_3.1[x$all_partisanship == "Democrat"]
x$a3understand[x$all_partisanship == "Republican"] <- 
  x$a3mechanisms_3[x$all_partisanship == "Republican"]

# a4
x$a4different <- NA
x$a4different[x$all_partisanship == "Democrat"] <- 
  x$a4mechanisms_1.1[x$all_partisanship == "Democrat"]
x$a4different[x$all_partisanship == "Republican"] <- 
  x$a4mechanisms_1[x$all_partisanship == "Republican"]

x$a4disagree <- NA
x$a4disagree[x$all_partisanship == "Democrat"] <- 
  x$a4mechanisms_2.1[x$all_partisanship == "Democrat"]
x$a4disagree[x$all_partisanship == "Republican"] <- 
  x$a4mechanisms_2[x$all_partisanship == "Republican"]

x$a4understand <- NA
x$a4understand[x$all_partisanship == "Democrat"] <- 
  x$a4mechanisms_3.1[x$all_partisanship == "Democrat"]
x$a4understand[x$all_partisanship == "Republican"] <- 
  x$a4mechanisms_3[x$all_partisanship == "Republican"]

# a5
x$a5different <- NA
x$a5different[x$all_partisanship == "Democrat"] <- 
  x$a5mechanisms_1.1[x$all_partisanship == "Democrat"]
x$a5different[x$all_partisanship == "Republican"] <- 
  x$a5mechanisms_1[x$all_partisanship == "Republican"]

x$a5disagree <- NA
x$a5disagree[x$all_partisanship == "Democrat"] <- 
  x$a5mechanisms_2.1[x$all_partisanship == "Democrat"]
x$a5disagree[x$all_partisanship == "Republican"] <- 
  x$a5mechanisms_2[x$all_partisanship == "Republican"]

x$a5understand <- NA
x$a5understand[x$all_partisanship == "Democrat"] <- 
  x$a5mechanisms_3.1[x$all_partisanship == "Democrat"]
x$a5understand[x$all_partisanship == "Republican"] <- 
  x$a5mechanisms_3[x$all_partisanship == "Republican"]

## Combine variables

x$different <- NA
x$different[x$a1outparty == "1" & !is.na(x$a1outparty)] <-
  x$a1different[x$a1outparty == "1" & !is.na(x$a1outparty)]
x$different[x$a2outparty == "1" & !is.na(x$a2outparty)] <-
  x$a2different[x$a2outparty == "1" & !is.na(x$a2outparty)]
x$different[x$a3outparty == "1" & !is.na(x$a3outparty)] <-
  x$a3different[x$a3outparty == "1" & !is.na(x$a3outparty)]
x$different[x$a4outparty == "1" & !is.na(x$a4outparty)] <-
  x$a4different[x$a4outparty == "1" & !is.na(x$a4outparty)]
x$different[x$a5outparty == "1" & !is.na(x$a5outparty)] <-
  x$a5different[x$a5outparty == "1" & !is.na(x$a5outparty)]

x$disagree <- NA
x$disagree[x$a1outparty == "1"& !is.na(x$a1outparty)] <-
  x$a1disagree[x$a1outparty == "1"& !is.na(x$a1outparty)]
x$disagree[x$a2outparty == "1" & !is.na(x$a2outparty)] <-
  x$a2disagree[x$a2outparty == "1" & !is.na(x$a2outparty)]
x$disagree[x$a3outparty == "1" & !is.na(x$a3outparty)] <-
  x$a3disagree[x$a3outparty == "1" & !is.na(x$a3outparty)]
x$disagree[x$a4outparty == "1" & !is.na(x$a4outparty)] <-
  x$a4disagree[x$a4outparty == "1" & !is.na(x$a4outparty)]
x$disagree[x$a5outparty == "1" & !is.na(x$a5outparty)] <-
  x$a5disagree[x$a5outparty == "1" & !is.na(x$a5outparty)]

x$understand <- NA
x$understand[x$a1outparty == "1"& !is.na(x$a1outparty)] <-
  x$a1understand[x$a1outparty == "1"& !is.na(x$a1outparty)]
x$understand[x$a2outparty == "1" & !is.na(x$a2outparty)] <-
  x$a2understand[x$a2outparty == "1" & !is.na(x$a2outparty)]
x$understand[x$a3outparty == "1" & !is.na(x$a3outparty)] <-
  x$a3understand[x$a3outparty == "1" & !is.na(x$a3outparty)]
x$understand[x$a4outparty == "1" & !is.na(x$a4outparty)] <-
  x$a4understand[x$a4outparty == "1" & !is.na(x$a4outparty)]
x$understand[x$a5outparty == "1" & !is.na(x$a5outparty)] <-
  x$a5understand[x$a5outparty == "1" & !is.na(x$a5outparty)]

# Make factors for distributions

x$different <- as.factor(x$different)
x$understand <- as.factor(x$understand)
x$disagree <- as.factor(x$disagree)

x <- x %>% 
  mutate(different = case_when(
    different == "Strongly disagree" | different == "Disagree" ~ "Disagree",
    different == "Neither agree nor disagree" ~ "Neutral",
    different == "Strongly agree" | different == "Agree" ~ "Agree"
  ))

x <- x %>% 
  mutate(understand = case_when(
    understand == "Strongly disagree" | understand == "Disagree" ~ "Disagree",
    understand == "Neither agree nor disagree" ~ "Neutral",
    understand == "Strongly agree" | understand == "Agree" ~ "Agree"
  ))

x <- x %>% 
  mutate(disagree = case_when(
    disagree == "Strongly disagree" | disagree == "Disagree" ~ "Disagree",
    disagree == "Neither agree nor disagree" ~ "Neutral",
    disagree == "Strongly agree" | disagree == "Agree" ~ "Agree"
  ))

x$different <- as.factor(x$different)
x$understand <- as.factor(x$understand)
x$disagree <- as.factor(x$disagree)

# Change reference category to 'neutral'
x$different <- factor(x$different, levels = c("Agree", "Neutral", "Disagree"))
x$understand <- factor(x$understand, levels = c("Agree", "Neutral", "Disagree"))
x$disagree <- factor(x$disagree, levels = c("Agree", "Neutral", "Disagree"))

# Plot distribution
mechs_different <- x %>% ggplot(aes(different, fill = different)) + 
  scale_fill_manual(values = c("black", "brown", "darkgrey")) +
  geom_bar(aes(y = after_stat(count/sum(count)))) + 
  scale_y_continuous(labels = scales::percent) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("'TIE is different from other Democrats/Republicans") + 
  ylab("Proportion")  + 
  #theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 


mechs_understand <- x %>% 
  ggplot(aes(understand, fill = understand)) + 
  scale_fill_manual(values = c("black", "brown", "darkgrey")) +
  geom_bar(aes(y = after_stat(count/sum(count)))) + 
  scale_y_continuous(labels = scales::percent) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("When TIE and I talk about politics, I usually understand their point of view") + 
  ylab("Proportion")  + 
  #theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 

mechs_disagree <- x %>% 
  ggplot(aes(disagree, fill = disagree)) + 
  scale_fill_manual(values = c("black", "brown", "darkgrey")) +
  geom_bar(aes(y = after_stat(count/sum(count)))) + 
  scale_y_continuous(labels = scales::percent) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  xlab("When TIE and I talk about politics, we usually end up disagreeing") + 
  ylab("Proportion")  + 
  #theme_mechs +
  theme(axis.text.x = element_text(size = 15, family = "CM Roman", color = "black"),
        legend.title = element_blank(), legend.position = "bottom") 


## Regression models

x$different <- relevel(x$different, ref = "Neutral")
x$understand <- relevel(x$understand, ref = "Neutral")
x$disagree <- relevel(x$disagree, ref = "Neutral")

# Regression models, that control for everything

M1_different <- lm(outparty_final ~ different + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, 
                   data = x)

M2_understand <- lm(outparty_final ~ understand + Gender + Age + Education + Race
                    + Ideology + Political_interest + Strong_partisan, 
                    data = x)

M3_disagree <- lm(outparty_final ~ disagree + Gender + Age + Education + Race
                  + Ideology + Political_interest + Strong_partisan, 
                  data = x)

M1 <- tidy(M2_understand)%>% filter(term == "understandAgree" | term =="understandDisagree") 
M2 <- tidy(M3_disagree) %>% filter(term == "disagreeAgree" | term =="disagreeDisagree")
M3 <- tidy(M1_different) %>% filter(term == "differentAgree" | term =="differentDisagree")

M1$outcome <- "Understand"
M2$outcome <- "Disagree"
M3$outcome <- "Different"

M1$value <- NA
M1$value[M1$term == "understandAgree"] <- "Agree"
M1$value[M1$term == "understandDisagree"] <- "Disagree"
M1 <- M1 %>% select(-term)

M2$value <- NA
M2$value[M2$term == "disagreeAgree"] <- "Agree"
M2$value[M2$term == "disagreeDisagree"] <- "Disagree"
M2 <- M2 %>% select(-term)

M3$value <- NA
M3$value[M3$term == "differentAgree"] <- "Agree"
M3$value[M3$term == "differentDisagree"] <- "Disagree"
M3 <- M3 %>% select(-term)

merged <- bind_rows(M1, M2, M3) 

colors <- c("darkgrey", "black")

Mechanisms_close_networks <- ggplot(merged, aes(x = outcome, y = estimate, color = value)) + 
  geom_errorbar(aes(ymin = estimate - std.error * 1.96, ymax = estimate + std.error * 1.96), 
                width = 0, size = 1, position = position_dodge(0.5)) +
  #theme_mechs +
  geom_point(size = 3, position = position_dodge(0.5)) + 
  geom_line(aes(group = interaction(outcome, value))) +
  coord_flip() +
  geom_hline(yintercept = 0, linetype = "dashed") +
  ggtitle("") +
  xlab("") +
  ylab("Index of out-partisan animosity (0-1) relative to reference category (neutral)") +
  theme(legend.title = element_blank(), legend.position = "bottom") + 
  theme(axis.title.x = element_text(hjust = 1)) +
  scale_color_discrete(name = "") + 
  scale_fill_manual(values = colors) +
  scale_color_manual(values = colors)

## Regression tables

M1_different <- lm(outparty_final ~ different + Gender + Age + Education + Race
                   + Ideology + Political_interest + Strong_partisan, 
                   data = x)

M2_understand <- lm(outparty_final ~ understand + Gender + Age + Education + Race
                    + Ideology + Political_interest + Strong_partisan, 
                    data = x)

M3_disagree <- lm(outparty_final ~ disagree + Gender + Age + Education + Race
                  + Ideology + Political_interest + Strong_partisan, 
                  data = x)

mechs_close_regression <- texreg(l = list(M1_different, M2_understand, M3_disagree),
                                 use.packages = F,
                                 stars = c(0.05, 0.01, 0.001),
                                 digits=3, include.ci = FALSE, booktabs=T,
                                 custom.model.names = c("Difference", "Understanding", "Disagreement"),
                                 threeparttable = TRUE,
                                 caption.above=T,
                                 caption="Linear regressions modeling the relationship between out-party animosity and outcomes
of political discussions with out-party close-ties.",
                                 float.pos="hp!")



####
####


## Talking politics and network heterogeneity

## Figure 6 main paper

Affect_close_hom_talk <- lm(outparty_final ~ talking_politics_total + total_ties + Gender + Age + Education + Race
                            + Ideology + Political_interest + Strong_partisan, 
                            data = subset(data, close_network == "Homogeneous"))


Affect_close_het_talk <- lm(outparty_final ~ talking_outparty_total + talking_inparty_total + total_ties + 
                              Gender + Age + Education + Race
                            + Ideology + Political_interest + Strong_partisan, 
                            data = subset(data, close_network == "Heterogeneous"))


hearing_other_side <- texreg(l = list(Affect_close_hom_talk, Affect_close_het_talk),
                                 use.packages = F,
                                 stars = c(0.05, 0.01, 0.001),
                                 digits=3, include.ci = FALSE, booktabs=T,
                                 custom.model.names = c("Homogeneous nw + talking", "Het. nw + talking"),
                                 threeparttable = TRUE,
                                 caption.above=T,
                                 caption="Linear regressions modeling the relationship between out-party animosity and talking politics with 
                                 close ties in homogeneous networks, and with out-partisan close ties in heterogeneous networks. Figure 6 in the main text",
                                 float.pos="hp!")

# Puzzle

puzzle_hom_talk <- lm(outcome_puzzle ~ treatment_puzzle * talking_politics_total + total_ties + Gender + Age + Education + Race
                            + Ideology + Political_interest + Strong_partisan, 
                            data = subset(data, close_network == "Homogeneous"))


puzzle_het_talk <- lm(outcome_puzzle ~ treatment_puzzle * talking_outparty_total + talking_inparty_total + total_ties + 
                              Gender + Age + Education + Race
                            + Ideology + Political_interest + Strong_partisan, 
                            data = subset(data, close_network == "Heterogeneous"))

hearing_other_side_puzzle <- texreg(l = list(puzzle_hom_talk, puzzle_het_talk),
                             use.packages = F,
                             stars = c(0.05, 0.01, 0.001),
                             digits=3, include.ci = FALSE, booktabs=T,
                             custom.model.names = c("Homogeneous nw + talking", "Het. nw + talking"),
                             threeparttable = TRUE,
                             caption.above=T,
                             caption="Linear regressions modeling the relationship between selecting the engineer in the competence game
                             and talking politics with close ties in homogeneous networks, and with out-partisan close ties in heterogeneous networks",
                             float.pos="hp!")

